package com.hrbu.ssm.controller;

import com.hrbu.ssm.bean.Dept;
import com.hrbu.ssm.request.RespBody;
import com.hrbu.ssm.request.Result;
import com.hrbu.ssm.service.DeptService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/3/25
 * @Blog: https://tanyajun.top
 */
@Controller
@RequestMapping("dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @ResponseBody
    @GetMapping("deptList")
    public RespBody getDeptList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        System.out.println(page + "," + limit);
        List<Dept> deptList = deptService.getDeptListByPage(page, limit);
        Long total = deptService.getTotal();
        return new RespBody("", 0, total, deptList);
    }

    @GetMapping("")
    public String gotoDeptPage() {
        return "dept/dept";
    }

    @PostMapping("insert")
    @ResponseBody
    public Result insertDept(Dept dept) {
        System.out.println("插入操作:" + dept);
        Integer num = deptService.insert(dept);
        if (num == 0) {
            return Result.error(0).setMessage("添加失败");
        }
        return Result.ok(1).setMessage("部门-" + dept.getName() + ": 添加成功");
    }

    @PostMapping("update")
    @ResponseBody
    public Result updateDept(Dept dept) {
        System.out.println("更新操作:" + dept);
        Integer num = deptService.update(dept);
        if (num == 0) {
            return Result.error(0).setMessage("修改失败");
        }
        return Result.ok(1).setMessage("部门ID-" + dept.getId() + ": 修改成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public Result deleteDept(Dept dept) {
        System.out.println("删除操作:" + dept);
        Integer num = deptService.delete(dept);
        if (num == 0) {
            return Result.error(0).setMessage("删除失败");
        }
        return Result.ok(1).setMessage("部门ID-" + dept.getId() + "=" + dept.getName() + ": 删除成功");
    }
}
